//
//  232. 用栈实现队列.swift
//  Top600
//
//  Created by rjb on 2021/8/10.
//

import Foundation
class MyQueue {
    var stack1: [Int] = []
    var stack2: [Int] = []
    // stack1 不断栈
    // stack2 不断出栈，当stak2为nil时，将stack1的倒入stack2;
    // 这样入队是o(1),出队是o(1)
    /** Initialize your data structure here. */
    init() {

    }
    
    /** Push element x to the back of queue. */
    func push(_ x: Int) {
        stack1.append(x)
    }
    
    /** Removes the element from in front of queue and returns that element. */
    func pop() -> Int {
        if stack2.isEmpty {
            moveToStack2()
        }
        return stack2.removeLast()
    }
    
    /** Get the front element. */
    func peek() -> Int {
        if stack2.isEmpty {
            moveToStack2()
        }
        return stack2.last!
    }
    
    /** Returns whether the queue is empty. */
    func empty() -> Bool {
        if stack1.isEmpty && stack2.isEmpty {
            return true
        }
        return false
    }
    func moveToStack2() {
        while !stack1.isEmpty {
            let top = stack1.removeLast()
            stack2.append(top)
        }
    }
}

// 队列有入队，出队，对顶元素，是否为空队列等操作
// 用栈实现队列，需要用到两个栈；

class Solution232 {
    
    static func test() {
        
    }
}
